
<body class="iframe">
    <div class="task">
        <div class="frame-header">
            <h2><i class="layui-icon">&#xe705;</i>任务管理</h2>
        </div>
        <h4 class="layui-bg-dark frame-title"><i class="layui-icon">&#xe65f;</i>添加任务</h4>
        <div class="layui-tab layui-tab-card">
            <ul class="layui-tab-title">
                <li class="layui-this">代理群发</li>
                <li>普通邮件</li>
                <li>域名群发</li>
                <li>域名普通邮件</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-tab-item  layui-show">
                    <form class="layui-form" action="" lay-filter='masssend'>
                        <div class="layui-form-item">
                            <label class="layui-form-label">发件人</label>
                            <div class="layui-input-block">
                                <select name="sender" lay-verify="required" lay-filter='sender'>
                                    <option value=""></option>
                                    <option value="2">今日可用</option>
                                    <option value="1">最新添加</option>
                                    <option value="0">全部发送</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">收件人</label>
                            <div class="layui-input-block">
                                <select name="receiver" lay-verify="required" lay-filter='receiver'>
                                    <option value=""></option>
                                    <?php foreach($group as $rows): ?>
                                    <option value="<?=$rows['id']?>"><?=$rows['name']?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">模板选择</label>
                            <div class="layui-input-inline">
                                <select name="templatetype" lay-verify="required" lay-filter='template'>
                                    <option value=""></option>
                                    <?php foreach ($template as $rows): ?>
                                        <option value="<?= $rows['id'] ?>"><?= $rows['brief'] ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">主题</label>
                            <div class="layui-input-block">
                                <input type="text" name="subject" required lay-verify="required" placeholder="请输入邮件主题" autocomplete="off" class="layui-input subject">
                            </div>
                        </div>
                        <div class="layui-form-item layui-form-text">
                            <label class="layui-form-label">正文</label>
                            <div class="layui-input-block">
                                <textarea name="content" placeholder="请输入邮件正文" class="layui-textarea content"></textarea>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button class="layui-btn send-btn" lay-submit lay-filter="masssend">发送</button>
                            </div>
                        </div>
                    </form>

                </div>
                <div class="layui-tab-item">
                    <form class="layui-form" action="">
                        <div class="layui-form-item">
                            <label class="layui-form-label">发件人</label>
                            <div class="layui-input-inline">
                                <input type="text" name="sender" required lay-verify="required" placeholder="请填写发件邮箱" autocomplete="off" class="layui-input">
                            </div>
                            <label class="layui-form-label">授权账号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="approve_username" required lay-verify="required" placeholder="请填写客户端授权账号" autocomplete="off" class="layui-input">
                            </div>
                            <label class="layui-form-label">授权密码</label>
                            <div class="layui-input-inline">
                                <input type="password" name="approve_password" required lay-verify="required" placeholder="请填写客户端授权密码" autocomplete="off" class="layui-input">
                            </div>
                            <label class="layui-form-label">渠道</label>
                            <div class="layui-input-inline">
                                <input type="text" name="smtp" required lay-verify="required" placeholder="请填写qq/163" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">收件人</label>
                            <div class="layui-input-block">
                                <input type="text" name="receiver" required lay-verify="required" placeholder="请填写收件邮箱,多个邮箱使用；分隔" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">主题</label>
                            <div class="layui-input-block">
                                <input type="text" name="subject" required lay-verify="required" placeholder="请输入主题" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">附件</label>
                            <div class="layui-input-inline">
                                <input type="text" placeholder="附件路径" autocomplete="off" class="layui-input localurl" >
                                <input type="hidden" name="attachment"  autocomplete="off" class="layui-input serverurl">
                            </div>
                            <button type="button" class="layui-btn" id="uploadattachment"><i class="layui-icon">&#xe67c;</i>上传附件</button>
                        </div>
                        <div class="layui-form-item layui-form-text">
                            <label class="layui-form-label">正文</label>
                            <div class="layui-input-block">
                                <textarea name="content" placeholder="请输入正文" class="layui-textarea"></textarea>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button class="layui-btn send-btn" lay-submit lay-filter="sendmail">发送</button>
                            </div>
                        </div>
                    </form>

                </div>
                <div class="layui-tab-item">
                    <form class="layui-form" action="" lay-filter='masssend'>
                        <div class="layui-form-item">
                            <label class="layui-form-label">收件人</label>
                            <div class="layui-input-block">
                                <select name="receiver" lay-verify="required" lay-filter='receiver'>
                                    <option value=""></option>
                                    <?php foreach($group as $rows): ?>
                                    <option value="<?=$rows['id']?>"><?=$rows['name']?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">模板选择</label>
                            <div class="layui-input-inline">
                                <select name="templatetype" lay-verify="required" lay-filter='template'>
                                    <option value=""></option>
                                    <?php foreach ($template as $rows): ?>
                                        <option value="<?= $rows['id'] ?>"><?= $rows['brief'] ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">主题</label>
                            <div class="layui-input-block">
                                <input type="text" name="subject" required lay-verify="required" placeholder="请输入邮件主题" autocomplete="off" class="layui-input subject">
                            </div>
                        </div>
                        <div class="layui-form-item layui-form-text">
                            <label class="layui-form-label">正文</label>
                            <div class="layui-input-block">
                                <textarea name="content" placeholder="请输入邮件正文" class="layui-textarea content"></textarea>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button class="layui-btn send-btn" lay-submit lay-filter="masssendviamailserver">发送</button>
                            </div>
                        </div>
                    </form>

                </div>
                <div class="layui-tab-item">
                    <form class="layui-form" action="">
                        <div class="layui-form-item">
                            <label class="layui-form-label">收件人</label>
                            <div class="layui-input-block">
                                <input type="text" name="receiver" required lay-verify="required" placeholder="请填写收件邮箱,多个邮箱使用；分隔" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">主题</label>
                            <div class="layui-input-block">
                                <input type="text" name="subject" required lay-verify="required" placeholder="请输入主题" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">附件</label>
                            <div class="layui-input-inline">
                                <input type="text" placeholder="附件路径" autocomplete="off" id="mailserver_localurl" class="layui-input" >
                                <input type="hidden" name="attachment"  autocomplete="off" id="mailserver_serverurl" class="layui-input">
                            </div>
                            <button type="button" class="layui-btn" id="mailserver_uploadattachment"><i class="layui-icon">&#xe67c;</i>上传附件</button>
                        </div>
                        <div class="layui-form-item layui-form-text">
                            <label class="layui-form-label">正文</label>
                            <div class="layui-input-block">
                                <textarea name="content" placeholder="请输入正文" class="layui-textarea"></textarea>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button class="layui-btn send-btn" lay-submit lay-filter="sendmailviamailserver">发送</button>
                            </div>
                        </div>
                    </form>

                </div>

            </div>
        </div>

    </div>
</body>
<script>
    layui.use(['element', 'form', 'layer', 'upload'], function () {
        var element = layui.element, form = layui.form, layer = layui.layer, upload = layui.upload;

        //上传文件
        var uploadInst = upload.render({
            elem: '#uploadattachment',
            accept: 'file',
            size: 102400, //最大支持100m的文件上传
            exts: 'png|gif|jpg|jpeg|txt|xls|mp4|wav|xlsx',
            auto: false,
            url: '<?= site_url("upload/do_upload") ?>',
            choose: function (obj) {
                layer.confirm('确定上传此附件?', function (index) {
                    obj.preview(function (index, file, result) {
                        $(".localurl").val(file.name);
                        layer.load();
                        obj.upload(index, file);
                    });
                });
            },
            done: function (redata) {
                if (redata.errno == '666') {
                    layer.closeAll('loading');
                    layer.msg(redata.error);
                    $(".localurl").val(redata.uploadInfo.upload_data.file_name);
                } else {
                    layer.msg(redata.error);
                }
            },
            error: function () {
                layer.msg('上传异常');
            }
        });
        //上传文件
        var uploadInst = upload.render({
            elem: '#mailserver_uploadattachment',
            accept: 'file',
            size: 102400, //最大支持100m的文件上传
            exts: 'png|gif|jpg|jpeg|txt|xls|mp4|wav|xlsx',
            auto: false,
            url: '<?= site_url("upload/do_upload") ?>',
            choose: function (obj) {
                layer.confirm('确定上传此附件?', function (index) {
                    obj.preview(function (index, file, result) {
                        $("#mailserver_localurl").val(file.name);
                        layer.load();
                        obj.upload(index, file);
                    });
                });
            },
            done: function (redata) {
                if (redata.errno == '666') {
                    layer.closeAll('loading');
                    layer.msg(redata.error);
                    $("#mailserver_serverurl").val(redata.uploadInfo.upload_data.file_name);
                } else {
                    layer.msg(redata.error);
                }
            },
            error: function () {
                layer.msg('上传异常');
            }
        });
        //添加模板
        form.on('submit(sendmail)', function (data) {
            layer.msg('正在努力发送中...', {
                icon: 16
                , shade: 0.01
                ,time:0
            });
            let sender = data.field.sender;//发件人邮箱
            let receiver = data.field.receiver;//收件人件人邮箱
            let approve_username = data.field.approve_username;//授权账号
            let approve_password = data.field.approve_password;//授权密码
            let subject = data.field.subject;//邮件正文
            let content = data.field.content;//邮件正文
            let attachment = data.field.attachment;//附件地址
            let smtp = data.field.smtp;//附件地址
            let jsonData = {
                'sender': sender,
                'receiver': receiver,
                'approve_username': approve_username,
                'approve_password': approve_password,
                'subject': subject,
                'content': content,
                'attachment': attachment,
                'smtp': smtp
            };
            let url = '<?= site_url('task/send_handy') ?>';
            $.post(url, jsonData, function (redata) {
                if (redata.errno == '666') {
                    layer.closeAll();
                    layer.msg(redata.error);
                } else {
                    layer.msg(redata.error);
                    return false;
                }
            }, 'json');
            return false;
        });
        form.on('select(template)', function (data) {
            let id = data.value;
            let url = '<?= site_url("template/get_template_byid") ?>';
            let jsonData = {
                'id': id
            };
            $.get(url, jsonData, function (redata) {
                if (redata.errno == '666') {
                    $(".subject").val(redata.template.subject);
                    $(".content").val(redata.template.content);
                } else {
                    layer.msg(redata.error);
                    return false;
                }
            }, 'json');
        });


        form.on('submit(masssend)', function (data) {
            layer.msg('正在努力发送中...', {
                icon: 16
                , shade: 0.01
                ,time:0
            });
            let sender = data.field.sender;
            let receiver = data.field.receiver;
            let templatetype = data.field.templatetype;
            let template = data.field.template;
            let subject = data.field.subject;
            let content = data.field.content;
            let jsonData = {
                'sender': sender,
                'receiver': receiver,
                'templatetype': templatetype,
                'template': template,
                'subject': subject,
                'content': content
            };
            let url = '<?= site_url("task/send") ?>';
            $.post(url, jsonData, function (redata) {
                if (redata.error == '666') {
                    layer.closeAll();
                    layer.msg(redata.error);
                } else {
                    layer.msg(redata.error);
                }
            }, 'json');
            return false;

        });
        
        
        form.on('submit(masssendviamailserver)', function (data) {
            layer.msg('正在努力发送中...', {
                icon: 16
                , shade: 0.01
                ,time:0
            });
            let sender = data.field.sender;
            let receiver = data.field.receiver;
            let templatetype = data.field.templatetype;
            let template = data.field.template;
            let subject = data.field.subject;
            let content = data.field.content;
            let jsonData = {
                'receiver': receiver,
                'templatetype': templatetype,
                'template': template,
                'subject': subject,
                'content': content
            };
            let url = '<?= site_url("task/masssend_via_mailserver") ?>';
            $.post(url, jsonData, function (redata) {
                if (redata.error == '666') {
                    layer.closeAll();
                    layer.msg(redata.error);
                } else {
                    layer.msg(redata.error);
                }
            }, 'json');
            return false;

        });
        form.on('submit(sendmailviamailserver)', function (data) {
            layer.msg('正在努力发送中...', {
                icon: 16
                , shade: 0.01
                ,time:0
            });
            let receiver = data.field.receiver;
            let subject = data.field.subject;
            let content = data.field.content;
            let attachment = data.field.attachment;
            let jsonData = {
                'receiver': receiver,
                'subject': subject,
                'content':content,
                'attachment': attachment
            };
            let url = '<?= site_url("task/send_via_mailserver") ?>';
            $.post(url, jsonData, function (redata) {
                if (redata.error == '666') {
                    layer.closeAll();
                    layer.msg(redata.error);
                } else {
                    layer.msg(redata.error);
                }
            }, 'json');
            return false;
        });
        



    });
</script>

</html>